package com.kairos.jetparksample.ui.weight

import android.content.Context
import android.graphics.Color
import android.graphics.drawable.Drawable
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.annotation.ColorRes
import androidx.constraintlayout.widget.ConstraintLayout
import androidx.core.content.ContextCompat
import com.kairos.jetparksample.R
import kotlinx.android.synthetic.main.title_home_layout.view.*

/**
 * Date 2020/6/4
 * Author Zzh
 * Des app首页title
 */
class HomeTitleLayout @JvmOverloads constructor(
    context: Context,
    attrs: AttributeSet? = null,
    defStyleAttr: Int = 0
) : ConstraintLayout(context, attrs, defStyleAttr), View.OnClickListener {
    private var leftSrc: Drawable? = null
    private var titleContent: String? = null
    private var activitySrc: Drawable? = null
    private var right0Src: Drawable? = null
    private var right1Src: Drawable? = null
    private var right2Src: Drawable? = null
    private var bg: Drawable? = null
    private var contentColor = 0
    private var tContentTv: TextView? = null
    private var tLeftIv: ImageView? = null
    private var rOneIv: ImageView? = null
    private var rightTv: TextView? = null
    var rTwoIv: ImageView? = null
    var rThreeIv: ImageView? = null
    private var rightText: String? = null
    private var rightTextColor = 0
    private var leftIsVisible = 0
    private var rActivityIv: ImageView? = null
    private var rZeroIv: ImageView? = null
    private var drawableStart: Drawable? = null
    private var left2Src: Drawable? = null
    private var tLeft2Iv: ImageView? = null
    private var tLeftTv: TextView? = null
    private var leftText: String? = null
    private var leftTextColor = 0
    private var visibleLine = 0
    private fun initTypeArray(context: Context, attrs: AttributeSet?) {
        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.HomeTitleLayout)
        leftSrc = typedArray.getDrawable(R.styleable.HomeTitleLayout_titleLeftSrc)
        leftText = typedArray.getString(R.styleable.HomeTitleLayout_titleLeftText)
        leftTextColor =
            typedArray.getColor(R.styleable.HomeTitleLayout_titleLeftTextColor, Color.WHITE)
        left2Src = typedArray.getDrawable(R.styleable.HomeTitleLayout_titleLeft2Src)
        titleContent = typedArray.getString(R.styleable.HomeTitleLayout_titleContent)
        contentColor = typedArray.getColor(
            R.styleable.HomeTitleLayout_titleContentColor,
            ContextCompat.getColor(context, R.color.text_primary)
        )
        activitySrc = typedArray.getDrawable(R.styleable.HomeTitleLayout_activitySrc)
        right0Src = typedArray.getDrawable(R.styleable.HomeTitleLayout_titleRight0Src)
        right1Src = typedArray.getDrawable(R.styleable.HomeTitleLayout_titleRight1Src)
        right2Src = typedArray.getDrawable(R.styleable.HomeTitleLayout_titleRight2Src)
        rightText = typedArray.getString(R.styleable.HomeTitleLayout_titleRightText)
        rightTextColor = typedArray.getColor(
            R.styleable.HomeTitleLayout_titleRightTextColor,
            ContextCompat.getColor(context, android.R.color.white)
        )
        bg = typedArray.getDrawable(R.styleable.HomeTitleLayout_titleBackground)
        leftIsVisible = typedArray.getInt(R.styleable.HomeTitleLayout_titleLeftSrcVisible, 1)
        visibleLine = typedArray.getInt(R.styleable.HomeTitleLayout_titleLineVisible, 0)
        drawableStart =
            typedArray.getDrawable(R.styleable.HomeTitleLayout_titleContentDrawableStart)
        typedArray.recycle()
    }

    private fun initView(context: Context) {
        LayoutInflater.from(context).inflate(R.layout.title_home_layout, this)
        tLeftIv = findViewById(R.id.title_left_iv)
        tLeftTv = findViewById(R.id.title_left_tv)
        tLeft2Iv = findViewById(R.id.title_left2_iv)
        tContentTv = findViewById(R.id.title_content_tv)
        rActivityIv = findViewById(R.id.title_right_activity_iv)
        rZeroIv = findViewById(R.id.title_right_zero_iv)
        rOneIv = findViewById(R.id.title_right_one_iv)
        rTwoIv = findViewById(R.id.title_right_two_iv)
        rThreeIv = findViewById(R.id.title_right_three_iv)
        rightTv = findViewById(R.id.title_right_tv)
        val titleLine = findViewById<View>(R.id.title_line)
        if (titleLine != null) titleLine.visibility = if (visibleLine == 0) GONE else VISIBLE
        tLeftIv?.setImageDrawable(leftSrc)
        tLeftIv?.visibility = if (leftIsVisible == 0) INVISIBLE else VISIBLE
        tLeftTv?.visibility = if (TextUtils.isEmpty(leftText)) GONE else VISIBLE
        tLeftTv?.text = leftText
        tLeftTv?.setTextColor(leftTextColor)
        if (left2Src != null) {
            tLeft2Iv?.setImageDrawable(left2Src)
        }
        tContentTv?.text = titleContent
        tContentTv?.setTextColor(contentColor)
        tContentTv?.setCompoundDrawablesWithIntrinsicBounds(drawableStart, null, null, null)
        if (activitySrc != null) {
            rActivityIv?.setVisibility(VISIBLE)
            rActivityIv?.setImageDrawable(activitySrc)
        }
        if (right0Src != null) {
            rZeroIv?.setVisibility(VISIBLE)
            rZeroIv?.setImageDrawable(right0Src)
        }
        if (right1Src != null) {
            rOneIv?.setVisibility(VISIBLE)
            rOneIv?.setImageDrawable(right1Src)
        }
        if (right2Src != null) {
            rTwoIv?.setVisibility(VISIBLE)
            rTwoIv?.setImageDrawable(right2Src)
        }
        if (!TextUtils.isEmpty(rightText)) {
            rightTv?.setVisibility(VISIBLE)
            rightTv?.setText(rightText)
            rightTv?.setTextColor(rightTextColor)
        }
        setBackground(background)
        tLeftIv?.setOnClickListener(this)
        tLeftTv?.setOnClickListener(this)
        tLeft2Iv?.setOnClickListener(this)
        rActivityIv?.setOnClickListener(this)
        rZeroIv?.setOnClickListener(this)
        rOneIv?.setOnClickListener(this)
        rTwoIv?.setOnClickListener(this)
        rThreeIv?.setOnClickListener(this)
        rightTv?.setOnClickListener(this)
    }

    fun setTitleDrawable(left: Drawable?, top: Drawable?, right: Drawable?, bottom: Drawable?) {
        tContentTv?.setCompoundDrawablesWithIntrinsicBounds(left, top, right, bottom)
    }

    var title: String?
        get() = tContentTv?.text.toString()
        set(title) {
            tContentTv?.text = title
        }

    fun setVisibleLeftIv(visibility: Int) {
        tLeftIv?.visibility = visibility
    }

    fun setVisibleLeft2Iv(visibility: Int) {
        tLeft2Iv?.visibility = visibility
    }

    fun setLeftIv(drawableId: Int) {
        tLeftIv?.setImageResource(drawableId)
    }

    fun setVisible0Iv(visibility: Int) {
        rZeroIv?.visibility = visibility
    }

    fun setVisibleActivityIv(visibility: Int) {
        rActivityIv?.visibility = visibility
    }

    fun setActivityIvImg(drawableId: Int) {
        rActivityIv?.setImageResource(drawableId)
    }

    fun gettLeft2Iv(): ImageView? {
        return tLeft2Iv
    }

    fun gettLeftIv(): ImageView? {
        return tLeftIv
    }

    fun setVisibleRightIv(visibility: Int) {
        rOneIv?.visibility = visibility
        rTwoIv?.visibility = visibility
    }

    fun setVisibleRight1Iv(visibility: Int) {
        rOneIv?.visibility = visibility
    }

    fun setVisibleRight2Iv(visibility: Int) {
        rTwoIv?.visibility = visibility
    }

    fun setVisibleROneIv(visibility: Int) {
        if (rOneIv?.visibility != visibility) {
            rOneIv?.visibility = visibility
        }
    }

    fun setVisibleRightTv(visibility: Int) {
        rightTv?.visibility = visibility
    }

    fun setRightText(text: String?) {
        rightTv?.visibility = VISIBLE
        rightTv?.text = text
    }

    fun getRightText(): String {
        return rightTv?.text.toString()
    }

    fun getRightTextView(): TextView? {
        return rightTv
    }

    val isRightTextShow: Boolean
        get() = rightTv?.visibility == VISIBLE

    fun setRightTextColor(@ColorRes id: Int) {
        rightTv?.visibility = VISIBLE
        val color = ContextCompat.getColor(context, id)
        rightTv?.setTextColor(color)
    }

    fun setR2SrcSelected(isSelect: Boolean) {
        rTwoIv?.isSelected = isSelect
    }

    fun setR1SrcSelected(isSelect: Boolean) {
        rOneIv?.isSelected = isSelect
    }

    fun setLeft2SrcSelected(isSelect: Boolean) {
        tLeft2Iv?.isSelected = isSelect
    }

    fun setRight3Src(imgid: Int) {
        rThreeIv?.visibility = VISIBLE
        rThreeIv?.setImageResource(imgid)
    }

    var listener: OnHomeTitleClickListener? = null

    init {
        initTypeArray(context, attrs)
        initView(context)
    }

    fun setOnHomeTitleClickListener(listener: OnHomeTitleClickListener?) {
        this.listener = listener
    }

    fun goBackCallBack(callBack: () -> Unit) {
        title_left_tv.setOnClickListener {
            callBack();
        }
        title_left_iv.setOnClickListener {
            callBack()
        }
    }

    fun onRight2ClickCallBack(callBack: () -> Unit) {
        rTwoIv?.setOnClickListener {
            callBack()
        }
    }

    fun onRight1ClickCallBack(callBack: () -> Unit) {
        rOneIv?.setOnClickListener {
            callBack()
        }
    }

    fun onRight0ClickCallBack(callBack: () -> Unit) {
        rZeroIv?.setOnClickListener {
            callBack()
        }
    }

    fun onRightTextViewClickCallBack(callBack: () -> Unit) {
        rightTv?.setOnClickListener { callBack() }
    }

    fun onLeftClickCallBack(callBack: () -> Unit) {
        tLeftIv?.setOnClickListener {
            callBack()
        }
    }

    fun onLeft2ClickCallBack(callBack: () -> Unit) {
        tLeft2Iv?.setOnClickListener {
            callBack()
        }
    }

    override fun onClick(v: View) {
        when (v.id) {
            R.id.title_left_tv -> if (tLeftTv?.visibility == VISIBLE) {
                if (listener != null) listener?.onLeftClick()
            }
            R.id.title_left_iv -> if (tLeftIv?.visibility == VISIBLE) {
                if (listener != null) listener?.onLeftClick()
            }
            R.id.title_left2_iv -> if (tLeft2Iv?.visibility == VISIBLE) {
                if (listener != null) listener?.onLeft2Click(tLeft2Iv)
            }
            R.id.title_right_activity_iv -> if (rActivityIv?.visibility == VISIBLE) {
                if (listener != null) listener?.onActivityClick()
            }
            R.id.title_right_zero_iv -> if (rZeroIv?.visibility == VISIBLE) {
                if (listener != null) listener?.onRight0Click()
            }
            R.id.title_right_one_iv -> if (rOneIv?.visibility == VISIBLE) {
                if (listener != null) listener?.onRight1Click()
            }
            R.id.title_right_tv, R.id.title_right_two_iv -> if (rTwoIv?.visibility == VISIBLE || rightTv?.visibility == VISIBLE) {
                if (listener != null) listener?.onRight2Click()
            }
            R.id.title_right_three_iv -> if (rThreeIv?.visibility == VISIBLE) {
                if (listener != null) listener?.onRight3Click()
            }
        }
    }

    val isShowROneIv: Boolean
        get() = rOneIv?.visibility == VISIBLE
    val isShowRTwoIv: Boolean
        get() = rTwoIv?.visibility == VISIBLE

    interface OnHomeTitleClickListener {
        fun onLeftClick()
        fun onLeft2Click(tLeft2Iv: ImageView?)
        fun onActivityClick()
        fun onRight0Click()
        fun onRight1Click()
        fun onRight2Click()
        fun onRight3Click()
    }

    fun setRight2Src(right2Src: Drawable?) {
        this.right2Src = right2Src
        rTwoIv?.setImageDrawable(right2Src)
    }

    fun setRight2Src(imgid: Int) {
        rTwoIv?.setImageResource(imgid)
    }
}